package com.doubletick.proxy.ipidea.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.doubletick.common.exception.ServiceException;
import com.doubletick.proxy.ipidea.domain.ProxyAccount;
import com.doubletick.proxy.ipidea.util.dto.IpIdeaResponseDto;
import com.doubletick.proxy.ipidea.util.dto.account.ProxyAccountAdd;
import com.doubletick.proxy.ipidea.util.dto.account.ProxyAccountEdit;
import com.doubletick.proxy.ipidea.util.dto.account.ProxyAccountResult;
import com.doubletick.proxy.ipidea.util.dto.account.QueryProxyAccountListDto;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 认证账号
 */
@Slf4j
public class ProxyAccountUtil {

    /**
     * 添加认证账户
     *
     * @param account   账户
     * @param password  密码
     * @param limitFlow 流量上限(M)
     * @return
     */
    public static IpIdeaResponseDto add(String account, String password, String limitFlow) {
        ProxyAccountAdd proxyAccount = ProxyAccountAdd.builder().account(account).password(password).limit_flow(limitFlow).build();
        String url = IpIdeaConstant.BASE_URL + "/proxy_account_add";
//        status:200 body:{"ret":0,"code":"1","msg":"success","ret_data":[],"timestamp":1720075873}
//        status:200 body:{"ret":1,"code":"10007","msg":"认证账号名已存在","ret_data":{},"timestamp":1720076204}
        return IpIdeaUtil.request(url, proxyAccount, "添加认证账号");
    }


    public static List<ProxyAccountResult> list() {
        return list(null);
    }

    /**
     * 获取认证账号列表 （禁用状态的不会返回）
     *
     * @param account
     * @return
     */
    public static List<ProxyAccountResult> list(String account) {
        String url = IpIdeaConstant.BASE_URL + "/proxy_account_list";
        QueryProxyAccountListDto queryDto = QueryProxyAccountListDto.builder().account(account).build();

        IpIdeaResponseDto dto = IpIdeaUtil.request(url, queryDto, "查询认证账号列表");
        if (!dto.isSuccess()) {
            throw new ServiceException(dto.getCode() + ":" + dto.getMsg());
        }
        Object o = dto.getRet_data();
        List<ProxyAccountResult> list = new ArrayList<>();
        if (o instanceof JSONArray) {
            JSONArray array = (JSONArray) o;
            for (int i = 0, len = array.size(); i < len; i++) {
                list.add(array.getObject(i, ProxyAccountResult.class));
//                JSONObject json = array.getJSONObject(i);
//                ProxyAccountResult bean = JSON.to(ProxyAccountResult.class, json);
//                if (Objects.equals(json.getString("consumed_flow"),"0.00000000")) {
//                    bean.setConsumed_flow(new BigDecimal("0"));
//                }
//                list.add(bean);
            }
        }
        return list;
    }

    public static IpIdeaResponseDto edit(ProxyAccountEdit edit) {
        String url = IpIdeaConstant.BASE_URL + "/proxy_account_edit";
        return IpIdeaUtil.request(url, edit, "编辑认证账号");
    }


    @SneakyThrows
    public static void main(String[] args) {

        for (int i = 30; i < 31; i++) {
            //ipidea_june_h5_9
            String account = "ipidea_june_h5_" + i;
            String password = RandomUtil.randomString(8);
//            log.info("@account:{}:password:{}", account, password);
            System.out.println(StrUtil.format("@account:{}:password:{}", account, password));
            IpIdeaResponseDto dto = ProxyAccountUtil.add(account, password, "0");
            if (!dto.isSuccess()) {
                log.info("添加认证账号dto:{}:{}", JSON.toJSONString(dto));
                System.exit(0);
            }
            Thread.sleep(2010);
        }
//




//        IpIdeaResponseDto dto = add("asdfgh1", "asdfgh123_password", "1", "remark");
//        log.info("添加认证账号dto:{}", JSON.toJSONString(dto));


        /*
        List<ProxyAccountResult> list = list("");
        log.info("认证账号列表dto:{}", JSON.toJSONString(list));
        list.forEach(item -> {
            log.info("认证账号:{}:{}", item.getConsumed_flow(), item);
        });
        */

/*        ProxyAccountResult asdfgh1 = list.stream().filter(item -> "asdfgh1".equals(item.getAccount())).findFirst().orElse(null);
        if (asdfgh1 != null) {
            //
            ProxyAccountEdit edit = new ProxyAccountEdit();
//            BeanUtils.copyProperties(asdfgh1, edit);
            //这个需要自己保存，账号列表不会返回
            edit.setId("738096");
            edit.setAccount("asdfgh1");
            edit.setPassword("asdfgh123_password_3");
            edit.setLimit_flow("3");
            edit.setState("1");
            edit.setRemark("remark:3");
            // &spec=1   接口调用有频率限制  间隔大概2s
            Thread.sleep(2000);
            edit(edit);
        }*/

    }

    private static void de(String s) {
        String url = IpIdeaConstant.BASE_URL + "/proxy_account_del";
        ProxyAccount proxyAccount = new ProxyAccount();
        IpIdeaResponseDto dto = IpIdeaUtil.request(url, proxyAccount, "添加认证账号");

    }
}
